package com.hengfoxwolf.definerecyclerviewlayoutmanagerdemo.define
import android.content.Context
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.hengfoxwolf.definerecyclerviewlayoutmanagerdemo.R

class VerticalItemDecoration(context: Context) : RecyclerView.ItemDecoration() {

    private val mContext = context

    private val leftMargin = mContext.resources.getDimension(R.dimen.dp_10).toInt()
    private val rightMargin = mContext.resources.getDimension(R.dimen.dp_10).toInt()
    private val topMargin = mContext.resources.getDimension(R.dimen.dp_10).toInt()
    private val bottomMargin = mContext.resources.getDimension(R.dimen.dp_10).toInt()

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        outRect.top = topMargin
        outRect.left = leftMargin
        outRect.right = rightMargin

        val itemCount = parent.adapter?.itemCount ?: 0
        val viewLayoutPosition = (view.layoutParams as RecyclerView.LayoutParams).viewLayoutPosition
        outRect.bottom = if (itemCount - 1 == viewLayoutPosition) bottomMargin else 0
    }

}